package com.dz.Incubator.service.impl;

import com.dz.Incubator.config.Constant;
import com.dz.Incubator.config.CustomException;
import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.dao.ContactWayDao;
import com.dz.Incubator.entity.ContactWayEntity;
import com.dz.Incubator.service.ContactWayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

@Service
public class ContactWayServiceImpl implements ContactWayService {

    private static final Logger logger = LoggerFactory.getLogger(ContactWayService.class);

    @Autowired
    private ContactWayDao contactWayDao;

    @Override
    public ContactWayEntity insert(ContactWayEntity contactWayEntity) {
        if (null == contactWayEntity) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        contactWayEntity.setCreateTime(new Date());
        contactWayEntity.setUpdateTime(new Date());
        contactWayEntity.setDelTag(Constant.PO_ENTITY_DELTAG_NO_TAG);
        int ret = contactWayDao.insert(contactWayEntity);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "添加失败");
        }
        return contactWayEntity;
    }

    @Override
    public ContactWayEntity update(ContactWayEntity contactWayEntity) {
        if (null == contactWayEntity) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        contactWayEntity.setUpdateTime(new Date());
        int ret = contactWayDao.update(contactWayEntity);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "修改失败");
        }

        return contactWayEntity;
    }

    @Override
    public Long delete(Long id) {
        if (null == id) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = contactWayDao.delete(id);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "删除失败");
        }
        return id;
    }

    @Override
    public List<ContactWayEntity> find(Long[] ids) {
        if (null == ids || ids.length <= 0 ) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        List<ContactWayEntity> datas = contactWayDao.find(ids);
        return datas;
    }

    @Override
    public ContactWayEntity get(Long id) {
        if (null == id) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        ContactWayEntity contactWayEntity = contactWayDao.get(id);
        return contactWayEntity;
    }

    @Override
    public List<ContactWayEntity> findAll() {
        List<ContactWayEntity> datas = contactWayDao.findAll();
        return datas;
    }

    @Override
    public Pagination findByPagination(Pagination pagination) {
        if (null == pagination) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        List<ContactWayEntity> datas = contactWayDao.findByPagination(pagination);
        pagination.setList(datas);
        return pagination;
    }

    @Override
    public List<ContactWayEntity> insertBatch(List<ContactWayEntity> list) {
        if (null == list || list.size() <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        for (ContactWayEntity entity : list) {
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
        }
        int ret = contactWayDao.insertBatch(list);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批量添加失败");
        }
        return list;
    }

    @Override
    public List<ContactWayEntity> updateBatch(List<ContactWayEntity> list) {
        if (null == list || list.size() <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = contactWayDao.updateBatch(list);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批修改失败");
        }
        return list;
    }

    @Override
    public List<Long> deleteBatch(Long[] ids) {
        if (null == ids || ids.length <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = contactWayDao.deleteBatch(ids);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批删除失败");
        }
        return Arrays.asList(ids);
    }
}